{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "\n",
    "sys.path.append(os.path.dirname(os.getcwd()))\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"1\"\n",
    "\n",
    "# from utils.plot_utils import overlay_colormap\n",
    "from datasets.generate_map_from_bbox_person import load_image, load_target\n",
    "from utils.coco import COCO_ID_TO_CHANNEL\n",
    "\n",
    "import numpy as np\n",
    "from pycocotools.coco import COCO\n",
    "from pathlib import Path\n",
    "import cv2\n",
    "import pprint\n",
    "from collections import defaultdict\n",
    "\n",
    "COCO_FOLDER = \"/mnt/ssd2/xxx/data/coco/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## pos_weight for cmap person\n",
    "recommend to use the second method instead for flexibility\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coco_folder = Path(COCO_FOLDER)\n",
    "# split = \"val2017\"\n",
    "# split = \"train2017\"\n",
    "# root = coco_folder / split\n",
    "# annFile = coco_folder / \"annotations\" / f\"instances_{split}.json\"\n",
    "# mask_folder = coco_folder / \"cmaps_person\" / split\n",
    "\n",
    "# # Load COCO dataset\n",
    "# coco = COCO(annFile)\n",
    "# ids = list(sorted(coco.imgs.keys()))\n",
    "\n",
    "# with open(mask_folder.parent / (split + \"-ids.txt\"), \"r\") as f:\n",
    "#     ids_person_str = f.read().splitlines()\n",
    "#     # strip left zeros and to int\n",
    "#     ids_person = list(map(int, ids_person_str))\n",
    "\n",
    "# ids_other = list(set(ids) - set(ids_person))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# res = {}\n",
    "# area_all = 0\n",
    "# area_centerness_all = 0\n",
    "# area_centerness_50_all = 0\n",
    "\n",
    "# for id in ids:\n",
    "#     tmp = {}\n",
    "#     img = load_image(coco, root, id)\n",
    "#     w, h = img.size\n",
    "#     area = w * h\n",
    "#     tmp[\"area\"] = area\n",
    "#     tmp[\"area_centerness\"] = 0\n",
    "#     tmp[\"area_centerness_50\"] = 0\n",
    "#     res[id] = tmp\n",
    "#     area_all += area\n",
    "\n",
    "\n",
    "# for id_str in ids_person_str:\n",
    "#     mask = (\n",
    "#         cv2.imread(mask_folder / (id_str + \".png\"), cv2.IMREAD_UNCHANGED).astype(\n",
    "#             np.float32\n",
    "#         )\n",
    "#         / 255.0\n",
    "#     )\n",
    "#     area_centerness = np.sum(mask > 0)\n",
    "#     area_centerness_50 = np.sum(mask > 0.5)\n",
    "\n",
    "#     res[int(id_str)][\"area_centerness\"] = area_centerness\n",
    "#     res[int(id_str)][\"area_centerness_50\"] = area_centerness_50\n",
    "#     area_centerness_all += area_centerness\n",
    "#     area_centerness_50_all += area_centerness_50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_ids_file(directory):\n",
    "    # List all files in the directory\n",
    "    files = os.listdir(directory)\n",
    "\n",
    "    # Filter out only the .png files and remove the extension to get the IDs\n",
    "    ids = [os.path.splitext(file)[0] for file in files if file.endswith(\".png\")]\n",
    "\n",
    "    # Sort the IDs numerically\n",
    "    ids.sort()\n",
    "\n",
    "    # Write the IDs to the output file\n",
    "    output_file = directory + \"-ids.txt\"\n",
    "    with open(output_file, \"w\") as f:\n",
    "        for id in ids:\n",
    "            f.write(id + \"\\n\")\n",
    "\n",
    "    print(f\"{output_file} file has been created successfully.\")\n",
    "\n",
    "\n",
    "# # Define the paths to the directories\n",
    "# train_dir = \"train2017\"\n",
    "# val_dir = \"val2017\"\n",
    "\n",
    "# # Generate train-ids.txt\n",
    "# generate_ids_file(train_dir)\n",
    "\n",
    "# # Generate val-ids.txt\n",
    "# generate_ids_file(val_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_pos_weight_alpha(\n",
    "    split=\"val2017_person\",\n",
    "    coco_folder=COCO_FOLDER,\n",
    "    mask_folder=\"cmaps_person\",\n",
    "    probas=np.linspace(0, 1, 11)[:-1],\n",
    "):\n",
    "    print(f\"Calculating pos_weight and alpha for {split}...\")\n",
    "\n",
    "    coco_folder = Path(coco_folder)\n",
    "    annFile = coco_folder / \"annotations\" / f\"instances_{split}.json\"\n",
    "    coco = COCO(annFile)\n",
    "\n",
    "    split = split.split(\"_\")[0]\n",
    "\n",
    "    root = coco_folder / split\n",
    "    mask_folder = coco_folder / mask_folder / split\n",
    "\n",
    "    ids = list(sorted(coco.imgs.keys()))\n",
    "\n",
    "    txt_path = mask_folder.parent / (split + \"-ids.txt\")\n",
    "    if not txt_path.exists():\n",
    "        generate_ids_file(str(mask_folder.absolute()))\n",
    "\n",
    "    with open(txt_path, \"r\") as f:\n",
    "        ids_person_str = f.read().splitlines()\n",
    "        # # strip left zeros and to int\n",
    "        # ids_person = list(map(int, ids_person_str))\n",
    "\n",
    "    area_all = 0\n",
    "    area_centerness_all = {proba: 0 for proba in probas}\n",
    "\n",
    "    # for id in ids:\n",
    "    #     img = load_image(coco, root, id)\n",
    "    #     w, h = img.size\n",
    "    #     area = w * h\n",
    "    #     area_all += area\n",
    "\n",
    "    for id_str in ids_person_str:\n",
    "        # get rid of preceding zeros in id_str\n",
    "        id = int(id_str)\n",
    "        img = load_image(coco, root, id)\n",
    "        w, h = img.size\n",
    "        area = w * h\n",
    "        area_all += area\n",
    "\n",
    "        try:\n",
    "            mask = (\n",
    "                cv2.imread(\n",
    "                    mask_folder / (id_str + \".png\"), cv2.IMREAD_UNCHANGED\n",
    "                ).astype(np.float32)\n",
    "                / 255.0\n",
    "            )\n",
    "        except:\n",
    "            print(f\"Error loading {mask_folder / (id_str + '.png')}\")\n",
    "        for proba in probas:\n",
    "            area_centerness_all[proba] += np.sum(mask > proba)\n",
    "\n",
    "    pos_weight_all = {k: area_all / v for k, v in area_centerness_all.items()}\n",
    "    alpha_all = {k: 1 - 1 / v for k, v in pos_weight_all.items()}\n",
    "\n",
    "    print(f\"\\npos_weight_all for {split}:\")\n",
    "    pprint.pprint(pos_weight_all)\n",
    "\n",
    "    print(f\"\\nalpha_all for {split}:\")\n",
    "    pprint.pprint(alpha_all)\n",
    "\n",
    "    return pos_weight_all, alpha_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculating pos_weight and alpha for val2017_person...\n",
      "loading annotations into memory...\n",
      "Done (t=0.12s)\n",
      "creating index...\n",
      "index created!\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 3.606717387392169,\n",
      " 0.1: 4.1283694509697035,\n",
      " 0.2: 5.282034376573399,\n",
      " 0.30000000000000004: 7.1494939578340055,\n",
      " 0.4: 10.446969868779867,\n",
      " 0.5: 16.128149160971073,\n",
      " 0.6000000000000001: 27.780025203336514,\n",
      " 0.7000000000000001: 53.39112951806463,\n",
      " 0.8: 133.78171998496938,\n",
      " 0.9: 557.5448191029582}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.7227395738031339,\n",
      " 0.1: 0.7577736169506069,\n",
      " 0.2: 0.8106790057188671,\n",
      " 0.30000000000000004: 0.8601299608199182,\n",
      " 0.4: 0.9042784642283273,\n",
      " 0.5: 0.9379966051889,\n",
      " 0.6000000000000001: 0.9640029124278874,\n",
      " 0.7000000000000001: 0.981270296975799,\n",
      " 0.8: 0.9925251372152163,\n",
      " 0.9: 0.998206422218022}\n"
     ]
    }
   ],
   "source": [
    "pos_weight_all, alpha_all = get_pos_weight_alpha(\"val2017_person\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculating pos_weight and alpha for val2017_person...\n",
      "loading annotations into memory...\n",
      "Done (t=0.12s)\n",
      "creating index...\n",
      "index created!\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 505.50716464378934,\n",
      " 0.1: 1133.5330843807433,\n",
      " 0.2: 1867.7884588024876,\n",
      " 0.30000000000000004: 2382.6298746123157,\n",
      " 0.4: 3289.6089829924517,\n",
      " 0.5: 3289.6089829924517,\n",
      " 0.6000000000000001: 5312.959682606213,\n",
      " 0.7000000000000001: 7673.270801152256,\n",
      " 0.8: 13809.523686088884,\n",
      " 0.9: 69032.05840375587}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9980217886709782,\n",
      " 0.1: 0.9991178025469399,\n",
      " 0.2: 0.9994646074638233,\n",
      " 0.30000000000000004: 0.9995802957015458,\n",
      " 0.4: 0.999696012503258,\n",
      " 0.5: 0.999696012503258,\n",
      " 0.6000000000000001: 0.999811780992189,\n",
      " 0.7000000000000001: 0.9998696774783643,\n",
      " 0.8: 0.9999275862062493,\n",
      " 0.9: 0.9999855139767939}\n"
     ]
    }
   ],
   "source": [
    "pos_weight_all, alpha_all = get_pos_weight_alpha(\n",
    "    split=\"val2017_person\", coco_folder=COCO_FOLDER, mask_folder=\"gmaps_kp_person\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### for etas and phis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "eta: 0.1, phi: 0.1\n",
      "Calculating pos_weight and alpha for val2017_person...\n",
      "loading annotations into memory...\n",
      "Done (t=0.15s)\n",
      "creating index...\n",
      "index created!\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 3.6053196327234907,\n",
      " 0.1: 3.607253893433745,\n",
      " 0.2: 3.622340511677038,\n",
      " 0.30000000000000004: 3.6814297209445215,\n",
      " 0.4: 3.841902438222434,\n",
      " 0.5: 4.202842222364503,\n",
      " 0.6000000000000001: 5.078715004900064,\n",
      " 0.7000000000000001: 7.161764223398528,\n",
      " 0.8: 13.94209510300004,\n",
      " 0.9: 51.131191496544396}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.722632082070185,\n",
      " 0.1: 0.7227808106825273,\n",
      " 0.2: 0.7239353956988905,\n",
      " 0.30000000000000004: 0.7283664022402045,\n",
      " 0.4: 0.7397122867954246,\n",
      " 0.5: 0.7620657766597282,\n",
      " 0.6000000000000001: 0.8030998000409205,\n",
      " 0.7000000000000001: 0.8603696004494459,\n",
      " 0.8: 0.9282747684180679,\n",
      " 0.9: 0.9804424663159359}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.3, phi: 0.3\n",
      "Calculating pos_weight and alpha for val2017_person...\n",
      "loading annotations into memory...\n",
      "Done (t=0.34s)\n",
      "creating index...\n",
      "index created!\n",
      "/mnt/ssd2/xxx/data/coco/cmaps_person_eta0.3_phi0.3/val2017-ids.txt file has been created successfully.\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 3.6053196327234907,\n",
      " 0.1: 3.67185544551531,\n",
      " 0.2: 3.9624827091422317,\n",
      " 0.30000000000000004: 4.561998040948953,\n",
      " 0.4: 5.737362204151713,\n",
      " 0.5: 7.848871399300244,\n",
      " 0.6000000000000001: 12.268413422989026,\n",
      " 0.7000000000000001: 22.087108341684942,\n",
      " 0.8: 53.23394346477656,\n",
      " 0.9: 221.38605245573657}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.722632082070185,\n",
      " 0.1: 0.7276581241177758,\n",
      " 0.2: 0.7476329681659425,\n",
      " 0.30000000000000004: 0.7807978015282121,\n",
      " 0.4: 0.8257038749834598,\n",
      " 0.5: 0.872593147584358,\n",
      " 0.6000000000000001: 0.9184898677993553,\n",
      " 0.7000000000000001: 0.9547247206592135,\n",
      " 0.8: 0.9812149930117112,\n",
      " 0.9: 0.9954830036088207}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.7, phi: 0.7\n",
      "Calculating pos_weight and alpha for val2017_person...\n",
      "loading annotations into memory...\n",
      "Done (t=0.11s)\n",
      "creating index...\n",
      "index created!\n",
      "/mnt/ssd2/xxx/data/coco/cmaps_person_eta0.7_phi0.7/val2017-ids.txt file has been created successfully.\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 3.632119569743739,\n",
      " 0.1: 5.142432939150151,\n",
      " 0.2: 7.602280346727176,\n",
      " 0.30000000000000004: 11.347007387895522,\n",
      " 0.4: 17.825256525333508,\n",
      " 0.5: 28.866265452829705,\n",
      " 0.6000000000000001: 51.387174374891,\n",
      " 0.7000000000000001: 100.56360072228269,\n",
      " 0.8: 253.57199293632343,\n",
      " 0.9: 1038.3472995875939}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.7246786674287393,\n",
      " 0.1: 0.8055395156664382,\n",
      " 0.2: 0.8684605204765297,\n",
      " 0.30000000000000004: 0.9118710364931325,\n",
      " 0.4: 0.9438998255885527,\n",
      " 0.5: 0.9653574861758928,\n",
      " 0.6000000000000001: 0.9805398912828991,\n",
      " 0.7000000000000001: 0.9900560442066747,\n",
      " 0.8: 0.9960563468054174,\n",
      " 0.9: 0.9990369310919408}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.9, phi: 0.9\n",
      "Calculating pos_weight and alpha for val2017_person...\n",
      "loading annotations into memory...\n",
      "Done (t=0.25s)\n",
      "creating index...\n",
      "index created!\n",
      "/mnt/ssd2/xxx/data/coco/cmaps_person_eta0.9_phi0.9/val2017-ids.txt file has been created successfully.\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 3.740838969250474,\n",
      " 0.1: 6.637185453141972,\n",
      " 0.2: 10.79753380393036,\n",
      " 0.30000000000000004: 17.016858946068727,\n",
      " 0.4: 27.689069439060816,\n",
      " 0.5: 45.80977555378166,\n",
      " 0.6000000000000001: 82.5969241049892,\n",
      " 0.7000000000000001: 162.540880989813,\n",
      " 0.8: 408.8764952780109,\n",
      " 0.9: 1637.8278077534853}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.7326802869035651,\n",
      " 0.1: 0.8493337263121659,\n",
      " 0.2: 0.9073862589218294,\n",
      " 0.30000000000000004: 0.9412347482476475,\n",
      " 0.4: 0.9638846656728266,\n",
      " 0.5: 0.9781705981330125,\n",
      " 0.6000000000000001: 0.987893011624393,\n",
      " 0.7000000000000001: 0.9938477016126013,\n",
      " 0.8: 0.9975542736949943,\n",
      " 0.9: 0.9993894352048085}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.1, phi: 0.9\n",
      "Calculating pos_weight and alpha for val2017_person...\n",
      "loading annotations into memory...\n",
      "Done (t=0.11s)\n",
      "creating index...\n",
      "index created!\n",
      "/mnt/ssd2/xxx/data/coco/cmaps_person_eta0.1_phi0.9/val2017-ids.txt file has been created successfully.\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 3.632558156701879,\n",
      " 0.1: 4.320198376516086,\n",
      " 0.2: 5.324498719429356,\n",
      " 0.30000000000000004: 6.651919614666955,\n",
      " 0.4: 8.62717098889808,\n",
      " 0.5: 11.53834707357455,\n",
      " 0.6000000000000001: 16.74588800172964,\n",
      " 0.7000000000000001: 27.00665803437798,\n",
      " 0.8: 57.155713041388,\n",
      " 0.9: 213.8280856811458}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.7247119091114749,\n",
      " 0.1: 0.7685291477734353,\n",
      " 0.2: 0.8121888927588712,\n",
      " 0.30000000000000004: 0.8496674557228444,\n",
      " 0.4: 0.8840871473062426,\n",
      " 0.5: 0.9133324735663197,\n",
      " 0.6000000000000001: 0.9402838475990815,\n",
      " 0.7000000000000001: 0.9629720938174928,\n",
      " 0.8: 0.9825039362333583,\n",
      " 0.9: 0.9953233458700501}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.3, phi: 0.7\n",
      "Calculating pos_weight and alpha for val2017_person...\n",
      "loading annotations into memory...\n",
      "Done (t=0.25s)\n",
      "creating index...\n",
      "index created!\n",
      "/mnt/ssd2/xxx/data/coco/cmaps_person_eta0.3_phi0.7/val2017-ids.txt file has been created successfully.\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 3.6100771904103075,\n",
      " 0.1: 4.187480269857318,\n",
      " 0.2: 5.295430768547612,\n",
      " 0.30000000000000004: 6.986821761474345,\n",
      " 0.4: 9.866114274125723,\n",
      " 0.5: 14.717416441904152,\n",
      " 0.6000000000000001: 24.574141471025538,\n",
      " 0.7000000000000001: 46.16883509495367,\n",
      " 0.8: 114.0760010743627,\n",
      " 0.9: 474.4664268492274}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.7229976127224182,\n",
      " 0.1: 0.7611929046691189,\n",
      " 0.2: 0.8111579503711892,\n",
      " 0.30000000000000004: 0.8568734062297043,\n",
      " 0.4: 0.8986429741015123,\n",
      " 0.5: 0.9320532918296209,\n",
      " 0.6000000000000001: 0.9593068184628519,\n",
      " 0.7000000000000001: 0.9783403675240379,\n",
      " 0.8: 0.9912339143151755,\n",
      " 0.9: 0.9978923693154842}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.7, phi: 0.3\n",
      "Calculating pos_weight and alpha for val2017_person...\n",
      "loading annotations into memory...\n",
      "Done (t=0.13s)\n",
      "creating index...\n",
      "index created!\n",
      "/mnt/ssd2/xxx/data/coco/cmaps_person_eta0.7_phi0.3/val2017-ids.txt file has been created successfully.\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 3.608738916527604,\n",
      " 0.1: 4.146886568158347,\n",
      " 0.2: 5.20232711751301,\n",
      " 0.30000000000000004: 6.85360747362174,\n",
      " 0.4: 9.710719315528952,\n",
      " 0.5: 14.551666897849273,\n",
      " 0.6000000000000001: 24.389591175987118,\n",
      " 0.7000000000000001: 45.88969827920362,\n",
      " 0.8: 113.08472993783958,\n",
      " 0.9: 463.9224889555249}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.7228948884553226,\n",
      " 0.1: 0.7588552318555207,\n",
      " 0.2: 0.8077783312330322,\n",
      " 0.30000000000000004: 0.854091439603331,\n",
      " 0.4: 0.8970210169291121,\n",
      " 0.5: 0.9312793505362743,\n",
      " 0.6000000000000001: 0.9589989027374697,\n",
      " 0.7000000000000001: 0.9782086168029311,\n",
      " 0.8: 0.9911570731030646,\n",
      " 0.9: 0.9978444675052262}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.9, phi: 0.1\n",
      "Calculating pos_weight and alpha for val2017_person...\n",
      "loading annotations into memory...\n",
      "Done (t=0.25s)\n",
      "creating index...\n",
      "index created!\n",
      "/mnt/ssd2/xxx/data/coco/cmaps_person_eta0.9_phi0.1/val2017-ids.txt file has been created successfully.\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 3.6338148866440165,\n",
      " 0.1: 4.248311270788046,\n",
      " 0.2: 5.173966789459496,\n",
      " 0.30000000000000004: 6.430108804010559,\n",
      " 0.4: 8.336400995363379,\n",
      " 0.5: 11.185043675579683,\n",
      " 0.6000000000000001: 16.317375728761213,\n",
      " 0.7000000000000001: 26.467373109929586,\n",
      " 0.8: 56.07792160837505,\n",
      " 0.9: 204.87041683479316}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.7248071156085931,\n",
      " 0.1: 0.7646123515298577,\n",
      " 0.2: 0.8067246967960406,\n",
      " 0.30000000000000004: 0.8444816362397656,\n",
      " 0.4: 0.8800441580777856,\n",
      " 0.5: 0.9105948953795057,\n",
      " 0.6000000000000001: 0.9387156356130608,\n",
      " 0.7000000000000001: 0.962217633164931,\n",
      " 0.8: 0.9821676700683812,\n",
      " 0.9: 0.9951188657911191}\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "ETAs = [0.1, 0.3, 0.7, 0.9, 0.1, 0.3, 0.7, 0.9]\n",
    "PHIs = [0.1, 0.3, 0.7, 0.9, 0.9, 0.7, 0.3, 0.1]\n",
    "\n",
    "for eta, phi in zip(ETAs, PHIs):\n",
    "    print(f\"\\n\\neta: {eta}, phi: {phi}\")\n",
    "    pos_weight_all, alpha_all = get_pos_weight_alpha(\n",
    "        split=\"val2017_person\", mask_folder=\"cmaps_person\" + f\"_eta{eta}_phi{phi}\"\n",
    "    )\n",
    "    print(\"\\n\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## pos_weight for arbitary task and n_classes/target_category_ids\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_pos_weight_alpha(\n",
    "    split=\"val2017\",\n",
    "    coco_folder=COCO_FOLDER,\n",
    "    mask_folder=\"cmaps\",\n",
    "    target_category_ids=None,\n",
    "    n_classes=80,\n",
    "    probas=np.linspace(0, 1, 11)[:-1],\n",
    "):\n",
    "    if target_category_ids:\n",
    "        # overwrite n_classes\n",
    "        n_classes = len(target_category_ids)\n",
    "    elif n_classes:\n",
    "        target_category_ids = list(sorted(COCO_ID_TO_CHANNEL.keys()))[:n_classes]\n",
    "\n",
    "    coco_folder = Path(coco_folder)\n",
    "    file_name = f\"instances_{split}.json\"\n",
    "    annFile = coco_folder / \"annotations\" / file_name\n",
    "    coco = COCO(annFile)\n",
    "    split = split.split(\"_\")[0]\n",
    "\n",
    "    img_id_to_cat_id = defaultdict(set)\n",
    "    for cat_id in target_category_ids:\n",
    "        img_ids = coco.getImgIds(catIds=cat_id)\n",
    "        for img_id in img_ids:\n",
    "            img_id_to_cat_id[img_id].add(cat_id)\n",
    "\n",
    "    area_all = 0\n",
    "    area_centerness_all = {proba: 0 for proba in probas}\n",
    "\n",
    "    print(f\"Calculating pos_weight and alpha for {split}...\")\n",
    "\n",
    "    for id, cat_ids in img_id_to_cat_id.items():\n",
    "\n",
    "        w, h = coco.imgs[id][\"width\"], coco.imgs[id][\"height\"]\n",
    "        area = w * h\n",
    "        area_all += area * len(cat_ids)\n",
    "\n",
    "        for category_id in cat_ids:\n",
    "            mask_path = (\n",
    "                coco_folder\n",
    "                / mask_folder\n",
    "                / str(category_id)\n",
    "                / split\n",
    "                / coco.loadImgs(id)[0][\"file_name\"]\n",
    "            ).with_suffix(\".png\")\n",
    "            # print(mask_path)\n",
    "            mask = (\n",
    "                cv2.imread(mask_path, cv2.IMREAD_UNCHANGED).astype(np.float32) / 255.0\n",
    "            )\n",
    "            for proba in probas:\n",
    "                area_centerness_all[proba] += np.sum(mask > proba)\n",
    "\n",
    "    pos_weight_all = {k: area_all / v for k, v in area_centerness_all.items()}\n",
    "\n",
    "    print(f\"\\npos_weight_all for {split}:\")\n",
    "    pprint.pprint(pos_weight_all)\n",
    "\n",
    "    alpha_all = {k: 1 - 1 / v for k, v in pos_weight_all.items()}\n",
    "\n",
    "    print(f\"\\nalpha_all for {split}:\")\n",
    "    pprint.pprint(alpha_all)\n",
    "\n",
    "    return pos_weight_all, alpha_all"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### cmaps results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading annotations into memory...\n",
      "Done (t=0.61s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 3.606717387392169,\n",
      " 0.1: 4.1283694509697035,\n",
      " 0.2: 5.282034376573399,\n",
      " 0.30000000000000004: 7.1494939578340055,\n",
      " 0.4: 10.446969868779867,\n",
      " 0.5: 16.128149160971073,\n",
      " 0.6000000000000001: 27.780025203336514,\n",
      " 0.7000000000000001: 53.39112951806463,\n",
      " 0.8: 133.78171998496938,\n",
      " 0.9: 557.5448191029582}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.7227395738031339,\n",
      " 0.1: 0.7577736169506069,\n",
      " 0.2: 0.8106790057188671,\n",
      " 0.30000000000000004: 0.8601299608199182,\n",
      " 0.4: 0.9042784642283273,\n",
      " 0.5: 0.9379966051889,\n",
      " 0.6000000000000001: 0.9640029124278874,\n",
      " 0.7000000000000001: 0.981270296975799,\n",
      " 0.8: 0.9925251372152163,\n",
      " 0.9: 0.998206422218022}\n"
     ]
    }
   ],
   "source": [
    "pos_weight_all, alpha_all = get_pos_weight_alpha(\"val2017\", n_classes=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading annotations into memory...\n",
      "Done (t=0.56s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 5.728126992973447,\n",
      " 0.1: 6.593768345766961,\n",
      " 0.2: 8.512155056516058,\n",
      " 0.30000000000000004: 11.615376293530874,\n",
      " 0.4: 17.08354843145427,\n",
      " 0.5: 26.48045742944857,\n",
      " 0.6000000000000001: 45.73752443268081,\n",
      " 0.7000000000000001: 88.04433363024589,\n",
      " 0.8: 220.82780443949892,\n",
      " 0.9: 921.3561475243769}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.8254228648864322,\n",
      " 0.1: 0.8483416541859594,\n",
      " 0.2: 0.8825209370176476,\n",
      " 0.30000000000000004: 0.9139072230869572,\n",
      " 0.4: 0.9414641516654234,\n",
      " 0.5: 0.9622363018968126,\n",
      " 0.6000000000000001: 0.9781361144398653,\n",
      " 0.7000000000000001: 0.988642085654261,\n",
      " 0.8: 0.995471584737537,\n",
      " 0.9: 0.9989146433735892}\n"
     ]
    }
   ],
   "source": [
    "pos_weight_all, alpha_all = get_pos_weight_alpha(\"val2017\", n_classes=80)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculating pos_weight and alpha for val2017...\n",
      "loading annotations into memory...\n",
      "Done (t=0.80s)\n",
      "creating index...\n",
      "index created!\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 5.728126992973447,\n",
      " 0.1: 6.593768345766961,\n",
      " 0.2: 8.512155056516058,\n",
      " 0.30000000000000004: 11.615376293530874,\n",
      " 0.4: 17.08354843145427,\n",
      " 0.5: 26.48045742944857,\n",
      " 0.6000000000000001: 45.73752443268081,\n",
      " 0.7000000000000001: 88.04433363024589,\n",
      " 0.8: 220.82780443949892,\n",
      " 0.9: 921.3561475243769}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.8254228648864322,\n",
      " 0.1: 0.8483416541859594,\n",
      " 0.2: 0.8825209370176476,\n",
      " 0.30000000000000004: 0.9139072230869572,\n",
      " 0.4: 0.9414641516654234,\n",
      " 0.5: 0.9622363018968126,\n",
      " 0.6000000000000001: 0.9781361144398653,\n",
      " 0.7000000000000001: 0.988642085654261,\n",
      " 0.8: 0.995471584737537,\n",
      " 0.9: 0.9989146433735892}\n"
     ]
    }
   ],
   "source": [
    "pos_weight_all, alpha_all = get_pos_weight_alpha(\"val2017\", n_classes=80)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### for etas and phis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "eta: 0.5, phi: 0.5\n",
      "loading annotations into memory...\n",
      "Done (t=0.60s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 18.832373124071527,\n",
      " 0.1: 21.83124077980194,\n",
      " 0.2: 28.3728618017323,\n",
      " 0.30000000000000004: 38.842043613408066,\n",
      " 0.4: 57.1817502398448,\n",
      " 0.5: 88.41995464852607,\n",
      " 0.6000000000000001: 151.73945210489055,\n",
      " 0.7000000000000001: 288.90813493657555,\n",
      " 0.8: 705.582332294142,\n",
      " 0.9: 2755.1167106495277}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9468999475843116,\n",
      " 0.1: 0.95419408314505,\n",
      " 0.2: 0.9647550533679706,\n",
      " 0.30000000000000004: 0.9742547016847793,\n",
      " 0.4: 0.9825119029094848,\n",
      " 0.5: 0.9886903357508489,\n",
      " 0.6000000000000001: 0.9934097560909292,\n",
      " 0.7000000000000001: 0.9965386921340255,\n",
      " 0.8: 0.9985827309525331,\n",
      " 0.9: 0.9996370389696616}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.7, phi: 0.7\n",
      "loading annotations into memory...\n",
      "Done (t=0.56s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 18.95875385816707,\n",
      " 0.1: 27.496931650095114,\n",
      " 0.2: 41.237934813078155,\n",
      " 0.30000000000000004: 61.940284282184614,\n",
      " 0.4: 97.36950001052989,\n",
      " 0.5: 157.01705802445179,\n",
      " 0.6000000000000001: 277.2582540906365,\n",
      " 0.7000000000000001: 534.0124080124081,\n",
      " 0.8: 1302.0218047230157,\n",
      " 0.9: 4897.012558632168}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9472539172415481,\n",
      " 0.1: 0.9636323058614236,\n",
      " 0.2: 0.9757504830313941,\n",
      " 0.30000000000000004: 0.9838554179789643,\n",
      " 0.4: 0.9897298435352769,\n",
      " 0.5: 0.9936312652104061,\n",
      " 0.6000000000000001: 0.9963932543567374,\n",
      " 0.7000000000000001: 0.998127384336027,\n",
      " 0.8: 0.999231963707234,\n",
      " 0.9: 0.9997957938665611}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.1, phi: 0.9\n",
      "loading annotations into memory...\n",
      "Done (t=0.68s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 19.006294840978775,\n",
      " 0.1: 22.779736364967285,\n",
      " 0.2: 28.13839165003891,\n",
      " 0.30000000000000004: 35.187023867638054,\n",
      " 0.4: 45.676625917008444,\n",
      " 0.5: 61.279880334759724,\n",
      " 0.6000000000000001: 88.87766137115709,\n",
      " 0.7000000000000001: 141.8481429861239,\n",
      " 0.8: 289.8447622715183,\n",
      " 0.9: 953.6598992250347}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9473858525100886,\n",
      " 0.1: 0.9561013356792887,\n",
      " 0.2: 0.9644613660781632,\n",
      " 0.30000000000000004: 0.9715804324980234,\n",
      " 0.4: 0.9781069643406468,\n",
      " 0.5: 0.9836814302747133,\n",
      " 0.6000000000000001: 0.9887485788377807,\n",
      " 0.7000000000000001: 0.992950207320671,\n",
      " 0.8: 0.9965498772785715,\n",
      " 0.9: 0.998951408147902}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.3, phi: 0.7\n",
      "loading annotations into memory...\n",
      "Done (t=0.56s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 18.840683179734903,\n",
      " 0.1: 22.080421682020592,\n",
      " 0.2: 28.175700075392065,\n",
      " 0.30000000000000004: 37.46091324729441,\n",
      " 0.4: 53.23277487014333,\n",
      " 0.5: 79.61241156733674,\n",
      " 0.6000000000000001: 132.52348740459266,\n",
      " 0.7000000000000001: 245.84942609937482,\n",
      " 0.8: 587.4495126422595,\n",
      " 0.9: 2224.2014981788193}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9469233684118417,\n",
      " 0.1: 0.9547110098529382,\n",
      " 0.2: 0.964508424020549,\n",
      " 0.30000000000000004: 0.9733055093078323,\n",
      " 0.4: 0.9812145806330891,\n",
      " 0.5: 0.9874391444711583,\n",
      " 0.6000000000000001: 0.9924541677887859,\n",
      " 0.7000000000000001: 0.995932469658905,\n",
      " 0.8: 0.9982977260539342,\n",
      " 0.9: 0.9995504004467136}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.7, phi: 0.3\n",
      "loading annotations into memory...\n",
      "Done (t=0.71s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 18.850834019661406,\n",
      " 0.1: 22.00191573095197,\n",
      " 0.2: 28.114827880665317,\n",
      " 0.30000000000000004: 37.495546554952085,\n",
      " 0.4: 53.52559157995012,\n",
      " 0.5: 80.47171561204725,\n",
      " 0.6000000000000001: 134.6450276243094,\n",
      " 0.7000000000000001: 251.3345965675235,\n",
      " 0.8: 610.6597482971376,\n",
      " 0.9: 2432.130157060194}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9469519492369939,\n",
      " 0.1: 0.9545494123226181,\n",
      " 0.2: 0.9644315802236263,\n",
      " 0.30000000000000004: 0.9733301660629367,\n",
      " 0.4: 0.9813173480108797,\n",
      " 0.5: 0.987573273511143,\n",
      " 0.6000000000000001: 0.9925730640214191,\n",
      " 0.7000000000000001: 0.9960212401569183,\n",
      " 0.8: 0.9983624268624409,\n",
      " 0.9: 0.9995888377942698}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.9, phi: 0.1\n",
      "loading annotations into memory...\n",
      "Done (t=0.59s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 19.000849521901756,\n",
      " 0.1: 22.661226650384894,\n",
      " 0.2: 28.07005815340182,\n",
      " 0.30000000000000004: 35.215087786492106,\n",
      " 0.4: 46.025640624533374,\n",
      " 0.5: 62.05786558111977,\n",
      " 0.6000000000000001: 90.80806051559466,\n",
      " 0.7000000000000001: 147.1254216330724,\n",
      " 0.8: 309.6889747957055,\n",
      " 0.9: 1132.3731150064727}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.947370774193684,\n",
      " 0.1: 0.9558717621323903,\n",
      " 0.2: 0.9643748511479728,\n",
      " 0.30000000000000004: 0.9716030808708197,\n",
      " 0.4: 0.9782729803120446,\n",
      " 0.5: 0.9838860071864245,\n",
      " 0.6000000000000001: 0.988987761721568,\n",
      " 0.7000000000000001: 0.993203078102342,\n",
      " 0.8: 0.9967709538233975,\n",
      " 0.9: 0.9991168988500806}\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "ETAs = [0.5, 0.7, 0.1, 0.3, 0.7, 0.9]\n",
    "PHIs = [0.5, 0.7, 0.9, 0.7, 0.3, 0.1]\n",
    "TARGET = [35]\n",
    "\n",
    "for eta, phi in zip(ETAs, PHIs):\n",
    "\n",
    "    if eta == 0.5 and phi == 0.5:\n",
    "        mask_folder = \"cmaps\"\n",
    "    else:\n",
    "        mask_folder = \"cmaps\" + f\"_{eta}_{phi}\"\n",
    "    print(f\"\\n\\neta: {eta}, phi: {phi}\")\n",
    "    pos_weight_all, alpha_all = get_pos_weight_alpha(\n",
    "        split=\"val2017\", mask_folder=mask_folder, target_category_ids=TARGET\n",
    "    )\n",
    "    print(\"\\n\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "eta: 0.5, phi: 0.5\n",
      "loading annotations into memory...\n",
      "Done (t=0.59s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 27.62880522108167,\n",
      " 0.1: 31.973630571985176,\n",
      " 0.2: 41.35031565693367,\n",
      " 0.30000000000000004: 56.409871994359165,\n",
      " 0.4: 82.78952724862106,\n",
      " 0.5: 127.70154041873946,\n",
      " 0.6000000000000001: 218.72840577148014,\n",
      " 0.7000000000000001: 416.06515703694436,\n",
      " 0.8: 1013.6886134557857,\n",
      " 0.9: 3864.69704344036}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9638058905552322,\n",
      " 0.1: 0.9687242273676551,\n",
      " 0.2: 0.9758163877563455,\n",
      " 0.30000000000000004: 0.9822726064668256,\n",
      " 0.4: 0.9879211775542944,\n",
      " 0.5: 0.9921692408977922,\n",
      " 0.6000000000000001: 0.9954281201087125,\n",
      " 0.7000000000000001: 0.9975965302955874,\n",
      " 0.8: 0.9990135037656279,\n",
      " 0.9: 0.9997412475056234}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.7, phi: 0.7\n",
      "loading annotations into memory...\n",
      "Done (t=0.58s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 27.79440292084214,\n",
      " 0.1: 39.98612510963948,\n",
      " 0.2: 59.66384971839863,\n",
      " 0.30000000000000004: 89.29948687735413,\n",
      " 0.4: 140.05680968577568,\n",
      " 0.5: 225.58124630313156,\n",
      " 0.6000000000000001: 398.1739831678685,\n",
      " 0.7000000000000001: 764.1466705926288,\n",
      " 0.8: 1856.1656374293489,\n",
      " 0.9: 6679.399811676083}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9640215332976219,\n",
      " 0.1: 0.9749913251844718,\n",
      " 0.2: 0.9832394321734216,\n",
      " 0.30000000000000004: 0.9888017273674437,\n",
      " 0.4: 0.9928600401348313,\n",
      " 0.5: 0.99556700738032,\n",
      " 0.6000000000000001: 0.9974885350568513,\n",
      " 0.7000000000000001: 0.9986913507072871,\n",
      " 0.8: 0.9994612549764768,\n",
      " 0.9: 0.999850285949607}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.1, phi: 0.9\n",
      "loading annotations into memory...\n",
      "Done (t=0.70s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 27.95473948927213,\n",
      " 0.1: 33.321236648373876,\n",
      " 0.2: 41.215818317463345,\n",
      " 0.30000000000000004: 51.63782612493691,\n",
      " 0.4: 67.17363669249373,\n",
      " 0.5: 90.04880531950816,\n",
      " 0.6000000000000001: 130.98619757460412,\n",
      " 0.7000000000000001: 211.15864060329432,\n",
      " 0.8: 443.092230684984,\n",
      " 0.9: 1566.4061800730185}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9642278905734837,\n",
      " 0.1: 0.969989109031198,\n",
      " 0.2: 0.9757374706891044,\n",
      " 0.30000000000000004: 0.9806343513071113,\n",
      " 0.4: 0.985113207364702,\n",
      " 0.5: 0.9888949109713134,\n",
      " 0.6000000000000001: 0.9923656078387156,\n",
      " 0.7000000000000001: 0.9952642241059,\n",
      " 0.8: 0.9977431335267285,\n",
      " 0.9: 0.9993615959814757}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.3, phi: 0.7\n",
      "loading annotations into memory...\n",
      "Done (t=0.58s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 27.652737405020968,\n",
      " 0.1: 32.29870128687463,\n",
      " 0.2: 41.14182492377946,\n",
      " 0.30000000000000004: 54.67746500658527,\n",
      " 0.4: 77.64561923283213,\n",
      " 0.5: 116.08565998500964,\n",
      " 0.6000000000000001: 193.53289789593154,\n",
      " 0.7000000000000001: 361.3723598276055,\n",
      " 0.8: 875.5777836294365,\n",
      " 0.9: 3395.1129227823867}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9638372149073955,\n",
      " 0.1: 0.9690390028032992,\n",
      " 0.2: 0.975693834635371,\n",
      " 0.30000000000000004: 0.9817109297243469,\n",
      " 0.4: 0.9871209733416981,\n",
      " 0.5: 0.991385671579684,\n",
      " 0.6000000000000001: 0.9948329198246298,\n",
      " 0.7000000000000001: 0.9972327712045352,\n",
      " 0.8: 0.998857897015323,\n",
      " 0.9: 0.9997054589868603}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.7, phi: 0.3\n",
      "loading annotations into memory...\n",
      "Done (t=0.58s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 27.637050789777785,\n",
      " 0.1: 32.23930836408964,\n",
      " 0.2: 40.93270174013198,\n",
      " 0.30000000000000004: 54.30362007669671,\n",
      " 0.4: 77.10026607577363,\n",
      " 0.5: 115.26973264418895,\n",
      " 0.6000000000000001: 191.78248229123483,\n",
      " 0.7000000000000001: 355.7671492577237,\n",
      " 0.8: 848.3992393315048,\n",
      " 0.9: 3095.6254800418947}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9638166891392813,\n",
      " 0.1: 0.9689819648515205,\n",
      " 0.2: 0.9755696556179295,\n",
      " 0.30000000000000004: 0.9815850214297384,\n",
      " 0.4: 0.9870298761483235,\n",
      " 0.5: 0.9913246957630519,\n",
      " 0.6000000000000001: 0.9947857594288438,\n",
      " 0.7000000000000001: 0.9971891727437836,\n",
      " 0.8: 0.9988213096456947,\n",
      " 0.9: 0.9996769635065846}\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "eta: 0.9, phi: 0.1\n",
      "loading annotations into memory...\n",
      "Done (t=0.68s)\n",
      "creating index...\n",
      "index created!\n",
      "Calculating pos_weight and alpha for val2017...\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 27.929145595686006,\n",
      " 0.1: 33.169983524559626,\n",
      " 0.2: 40.816444829070356,\n",
      " 0.30000000000000004: 50.98448656302334,\n",
      " 0.4: 66.20690537020744,\n",
      " 0.5: 88.65536479792699,\n",
      " 0.6000000000000001: 128.72645149114243,\n",
      " 0.7000000000000001: 206.2110486637338,\n",
      " 0.8: 420.78578462195543,\n",
      " 0.9: 1321.757978161762}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.964195109493272,\n",
      " 0.1: 0.9698522611788583,\n",
      " 0.2: 0.9755000710084437,\n",
      " 0.30000000000000004: 0.9803861906353833,\n",
      " 0.4: 0.9848958353451452,\n",
      " 0.5: 0.9887203667563795,\n",
      " 0.6000000000000001: 0.9922315888582635,\n",
      " 0.7000000000000001: 0.9951505993181253,\n",
      " 0.8: 0.9976234938618508,\n",
      " 0.9: 0.9992434318411373}\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "ETAs = [0.5, 0.7, 0.1, 0.3, 0.7, 0.9]\n",
    "PHIs = [0.5, 0.7, 0.9, 0.7, 0.3, 0.1]\n",
    "TARGET = [44]\n",
    "\n",
    "for eta, phi in zip(ETAs, PHIs):\n",
    "\n",
    "    if eta == 0.5 and phi == 0.5:\n",
    "        mask_folder = \"cmaps\"\n",
    "    else:\n",
    "        mask_folder = \"cmaps\" + f\"_{eta}_{phi}\"\n",
    "\n",
    "    print(f\"\\n\\neta: {eta}, phi: {phi}\")\n",
    "    pos_weight_all, alpha_all = get_pos_weight_alpha(\n",
    "        split=\"val2017\", mask_folder=mask_folder, target_category_ids=TARGET\n",
    "    )\n",
    "\n",
    "    print(\"\\n\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### gmaps_kp results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculating pos_weight and alpha for val2017...\n",
      "loading annotations into memory...\n",
      "Done (t=0.56s)\n",
      "creating index...\n",
      "index created!\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 817.9442287341551,\n",
      " 0.1: 1824.244637718209,\n",
      " 0.2: 3001.1178175576115,\n",
      " 0.30000000000000004: 3826.4083116008924,\n",
      " 0.4: 5280.575006560302,\n",
      " 0.5: 5280.575006560302,\n",
      " 0.6000000000000001: 8525.738462222862,\n",
      " 0.7000000000000001: 12311.391255536793,\n",
      " 0.8: 22157.00689818434,\n",
      " 0.9: 110771.00836704925}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.9987774227571143,\n",
      " 0.1: 0.999451827907659,\n",
      " 0.2: 0.9996667908223563,\n",
      " 0.30000000000000004: 0.9997386583138637,\n",
      " 0.4: 0.9998106266838824,\n",
      " 0.5: 0.9998106266838824,\n",
      " 0.6000000000000001: 0.9998827081074054,\n",
      " 0.7000000000000001: 0.9999187744114988,\n",
      " 0.8: 0.99995486755027,\n",
      " 0.9: 0.9999909723670954}\n"
     ]
    }
   ],
   "source": [
    "pos_weight_all, alpha_all = get_pos_weight_alpha(\n",
    "    split=\"val2017\", mask_folder=\"gmaps_kp\", n_classes=80\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### emaps results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculating pos_weight and alpha for val2017...\n",
      "loading annotations into memory...\n",
      "Done (t=0.26s)\n",
      "creating index...\n",
      "index created!\n",
      "\n",
      "pos_weight_all for val2017:\n",
      "{0.0: 4.524946817714009,\n",
      " 0.1: 5.47351800388535,\n",
      " 0.2: 6.855364088932975,\n",
      " 0.30000000000000004: 8.794760773555128,\n",
      " 0.4: 11.914149416715203,\n",
      " 0.5: 16.893427081918016,\n",
      " 0.6000000000000001: 26.416160857037642,\n",
      " 0.7000000000000001: 46.11852285013789,\n",
      " 0.8: 104.54111036126541,\n",
      " 0.9: 398.2845297267721}\n",
      "\n",
      "alpha_all for val2017:\n",
      "{0.0: 0.7790029274851904,\n",
      " 0.1: 0.8173021447467324,\n",
      " 0.2: 0.8541288271451024,\n",
      " 0.30000000000000004: 0.8862959407597658,\n",
      " 0.4: 0.9160661860932322,\n",
      " 0.5: 0.9408053797994395,\n",
      " 0.6000000000000001: 0.9621443855747273,\n",
      " 0.7000000000000001: 0.9783167383038373,\n",
      " 0.8: 0.990434385128068,\n",
      " 0.9: 0.9974892321308939}\n"
     ]
    }
   ],
   "source": [
    "pos_weight_all, alpha_all = get_pos_weight_alpha(\n",
    "    split=\"val2017\", mask_folder=\"emaps\", n_classes=1\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ocdet",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
